/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlserver;

import Database.Database;
import Database.Table;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.Object;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author PHAMDANH
 */
public final class ShowDataPanel extends JScrollPane{
    private Database database;
    private JTable jtable;
    private JPopupMenu popup;
    private Table table;
    private int rowSelected = 0;
    public ShowDataPanel(Database database, Table table)
    {
        this.database = database;  
        this.table = table;        
        jtable = new JTable();
        InitTableModel();
        popup = new JPopupMenu();        
        popup.add(act_deleteRow);
        jtable.add(popup);
        jtable.addMouseListener(new PopupTrigger());
        this.setViewportView(jtable);
    }
    public Database getDatabase()
    {
        return database;
    }
    public Table getTable()
    {
        return table;
    }
    int editRowIndex=-1;
    List<String> editRow;
    boolean hasEditRow = false;
    public void clearEditRow()
    {
        if(hasEditRow)
        {
            editRow.clear();
            editRowIndex = -1;
            hasEditRow = false;
        }
    }
    public void newEditRow(int index)
    {
        editRow = new ArrayList<String>();
        editRowIndex= index;
        int nCols = table.getColumns().size();
        for(int i=0;i<nCols;i++)
        {
            editRow.add("NULL");
        }
        hasEditRow = true;
    }
    public void setEditRow(int idx, String value)
    {
        editRow.set(idx, value);
    }
    public void InitTableModel()
    { 
        int nCols = table.getColumns().size();
        int nRows = table.getColumns().get(0).getData().size();
        String[] columnNames = new String[nCols];   
        Object[][] data;
        if(hasEditRow)
            data= new Object[nRows+2][nCols];
        else
            data= new Object[nRows+1][nCols];
        for(int i=0;i<nCols;i++)
        {
            columnNames[i]=(table.getColumns().get(i).getName());
            for(int j=0;j<nRows;j++)
            {
                Object value = table.getColumns().get(i).getData().get(j);
                if(value==null)
                    data[j][i] = "NULL";
                else
                    data[j][i] = value.toString();
            }
        }
        for(int i=0;i<nCols;i++)
        {
            if(hasEditRow)
            {
                data[nRows][i]=editRow.get(i);
                data[nRows+1][i]="NULL";
            }
            else
                data[nRows][i]="NULL";
        }
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        jtable.setModel(model);
        for(int i=0;i<nCols;i++)
        {
            TableColumn col = jtable.getColumnModel().getColumn(i);
            col.setCellRenderer(new EditCellRenderer(this));
        }
    }
    private AbstractAction act_deleteRow = new AbstractAction("Delete row") 
    { 
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if(!table.RemoveRow(rowSelected))
                JOptionPane.showMessageDialog(null, "Không thể delete row");
            else
                InitTableModel(); 
        }
    };
    
    private class PopupTrigger extends MouseAdapter
    {
        @Override
        public void mouseReleased(MouseEvent e)
        {
            int x = e.getX();
            int y = e.getY();
            if (e.isPopupTrigger())
            {
                JTable source = (JTable)e.getSource();
                rowSelected = source.rowAtPoint( e.getPoint() );
                if(rowSelected!=-1)
                {
                    popup.show(jtable, x, y);
                }                
            }
        }
    }
    
}
